package com.rimi.servlet;

import com.rimi.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Set;

/**
 * @author chengliang
 * 创建时间 2020/3/24 16:10
 */
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Set<User> onlineUser = (Set<User>) session.getServletContext().getAttribute("onlineUser");
        User user = (User) session.getAttribute("user");
        onlineUser.remove(user);
        System.out.println("在线用户列表数量:"+onlineUser.size());
        //
        session.getServletContext().setAttribute("onlineUser",onlineUser);
        session.invalidate();//销毁session
        resp.sendRedirect("/index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
